// subcontract/Home/swiper/index.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    s: app.system.statusBarHeight, //状态栏高度
    n: (app.menu.top - app.system.statusBarHeight) * 2 + app.menu.height, //导航栏高度
    h: app.menu.height, //胶囊高度
    page: 1,//页码
    pagingState: false,//分页状态
    goodsList: [],//商品
    carousels: [
      { slides: ['Slide 1-1', 'Slide 1-2', 'Slide 1-3'], currentIndex: 0, translateX: '0%', interval: null },
      { slides: ['Slide 2-1', 'Slide 2-2', 'Slide 2-3'], currentIndex: 0, translateX: '0%', interval: null },
      { slides: ['Slide 3-1', 'Slide 3-2', 'Slide 3-3'], currentIndex: 0, translateX: '0%', interval: null }
    ],
  },
  /**返回 */
  navigateBack() {
    wx.navigateBack()
  },
  animationfinish(event) {
    let index = event.detail.current
    let length = this.data.goodsList.length
    console.log(index, length);
    if ((length - 3) == (index - 2)) {
      console.log(1);
      this.getGoodsList()
    }

  },
  /**商品列表 */
  async getGoodsList(id) {
    let object = {
      page_no: this.data.page,
      second_cate_id: id
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.getGoodsList(object)
      if (code == 1) {
        this.setData({
          goodsList: this.data.goodsList.concat(data.lists)
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
      }
    }
  },
  /** 跳转 */
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    console.log(url);
    wx.navigateTo({
      url,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.setData({
      secondcateId: options.id
    })
    this.getGoodsList(options.id)//商品列表
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  startAutoSlide(index) {
    const carousels = this.data.carousels;
    const interval = setInterval(() => {
      this.nextSlide(index);
    }, 3000); // 每3秒切换一次
    carousels[index].interval = interval; // 保存定时器
    this.setData({ carousels });
  },

  stopAutoSlide(index) {
    const carousels = this.data.carousels;
    clearInterval(carousels[index].interval); // 清除定时器
    carousels[index].interval = null; // 清空定时器引用
    this.setData({ carousels });
  },

  startAutoPlay(event) {
    const index = parseInt(event.currentTarget.dataset.index); // 获取点击的下标

    // 停止所有轮播的自动播放
    this.data.carousels.forEach((_, i) => {
      if (i !== index) {
        this.stopAutoSlide(i);
      }
    });

    this.stopAutoSlide(index); // 停止当前的自动播放
    this.startAutoSlide(index); // 启动新的自动播放
  },

  nextSlide(index) {
    const carousels = this.data.carousels;
    const { currentIndex, slides } = carousels[index];
    const newIndex = (currentIndex + 1) % slides.length; // 自动循环
    carousels[index].currentIndex = newIndex;
    carousels[index].translateX = `${-newIndex * 100}%`;
    this.setData({ carousels });
  },

  prevSlide(index) {
    const carousels = this.data.carousels;
    const { currentIndex, slides } = carousels[index];
    const newIndex = (currentIndex - 1 + slides.length) % slides.length; // 自动循环
    carousels[index].currentIndex = newIndex;
    carousels[index].translateX = `${-newIndex * 100}%`;
    this.setData({ carousels });
  },

  touchStart(event) {
    const index = event.currentTarget.dataset.index;
    this.stopAutoSlide(index); // 停止自动轮播
    this.setData({
      startX: event.touches[0].clientX,
      currentTouchIndex: index
    });
  },

  touchMove(event) {
    const index = this.data.currentTouchIndex;
    const moveX = event.touches[0].clientX - this.data.startX;
    const carousels = this.data.carousels;
    carousels[index].moveX = moveX; // 记录移动距离
    this.setData({ carousels });
  },

  touchEnd(event) {
    const index = this.data.currentTouchIndex;
    const { currentIndex, slides } = this.data.carousels[index];
    const moveX = this.data.carousels[index].moveX;

    if (moveX > 50) {
      this.prevSlide(index); // 向右滑动，切换到上一页
    } else if (moveX < -50) {
      this.nextSlide(index); // 向左滑动，切换到下一页
    }

    // 检查是否滑动到最后一张幻灯片
    if (this.data.carousels[index].currentIndex === slides.length - 1) {
      console.log(`当前播放的是第 ${this.data.carousels[index].currentIndex + 1} 张幻灯片`);
    }

    this.setData({
      [`carousels[${index}].moveX`]: 0 // 重置移动距离
    });
    this.startAutoSlide(index); // 重新启动自动轮播
  },

  onUnload() {
    // 清除所有定时器
    this.data.carousels.forEach((_, index) => {
      this.stopAutoSlide(index);
    });
  }
})